const { expect } = require('chai');
require('./setup.js');

describe('Plugin Integration', () => {
    beforeEach(() => {
        // Clear DOM
        if (document.body) {
            document.body.innerHTML = '';
        }
    });

    it('should create button element when gitbook starts', (done) => {
        // Import the plugin (this will execute the require callback)
        require('../src/plugin.js');
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
            expect(button).to.not.be.null;
            expect(button.textContent).to.equal('Test Report');
            expect(button.getAttribute('aria-label')).to.equal('Report an issue on GitCode');
            done();
        }, 200);
    });

    it('should handle button click', (done) => {
        require('../src/plugin.js');
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
            
            // Mock window.open
            let openedURL = '';
            global.window.open = (url) => {
                openedURL = url;
            };
            
            // Simulate button click
            button.click();
            
            setTimeout(() => {
                expect(openedURL).to.include('gitcode.com');
                expect(openedURL).to.include('test/repo');
                done();
            }, 100);
        }, 200);
    });

    it('should show loading state during processing', (done) => {
        require('../src/plugin.js');
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
            
            // Mock window.open to delay
            global.window.open = (url) => {
                return new Promise(resolve => setTimeout(resolve, 100));
            };
            
            button.click();
            
            // Check loading state
            setTimeout(() => {
                expect(button.classList.contains('loading')).to.be.true;
                expect(button.textContent).to.equal('Loading...');
                done();
            }, 50);
        }, 200);
    });

    it('should handle missing configuration gracefully', (done) => {
        // Mock gitbook with missing config
        global.window.require = (deps, callback) => {
            if (deps.includes('gitbook')) {
                const mockGitbook = {
                    events: {
                        bind: (event, handler) => {
                            if (event === 'start') {
                                setTimeout(() => {
                                    handler({}, {}); // No config
                                }, 100);
                            }
                        }
                    }
                };
                callback(mockGitbook);
            }
        };

        require('../src/plugin.js');
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
            expect(button).to.be.null;
            done();
        }, 200);
    });
});
